{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 3: Introduction to Data Analysis in Python Polars "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inspecting a DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pl.read_csv('../data/covid_19_deaths.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>Month</th><th>State</th><th>Sex</th><th>Age Group</th><th>COVID-19 Deaths</th><th>Total Deaths</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>null</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>1146774</td><td>12303399</td><td>1162844</td><td>569264</td><td>22229</td><td>1760095</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>null</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;Under 1 year&quot;</td><td>519</td><td>73213</td><td>1056</td><td>95</td><td>64</td><td>1541</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>null</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;0-17 years&quot;</td><td>1696</td><td>130970</td><td>2961</td><td>424</td><td>509</td><td>4716</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>null</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;1-4 years&quot;</td><td>285</td><td>14299</td><td>692</td><td>66</td><td>177</td><td>1079</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>null</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;5-14 years&quot;</td><td>509</td><td>22008</td><td>818</td><td>143</td><td>219</td><td>1390</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────────┬───────────┬───────────┬──────────┬───┬───────────┬───────────┬───────────┬──────────┐\n",
       "│ Data As Of ┆ Start     ┆ End Date  ┆ Group    ┆ … ┆ Pneumonia ┆ Influenza ┆ Pneumonia ┆ Footnote │\n",
       "│ ---        ┆ Date      ┆ ---       ┆ ---      ┆   ┆ and       ┆ Deaths    ┆ , Influen ┆ ---      │\n",
       "│ str        ┆ ---       ┆ str       ┆ str      ┆   ┆ COVID-19  ┆ ---       ┆ za, or    ┆ str      │\n",
       "│            ┆ str       ┆           ┆          ┆   ┆ Deaths    ┆ i64       ┆ COVID…    ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ ---       ┆           ┆ ---       ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ i64       ┆           ┆ i64       ┆          │\n",
       "╞════════════╪═══════════╪═══════════╪══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 569264    ┆ 22229     ┆ 1760095   ┆ null     │\n",
       "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 95        ┆ 64        ┆ 1541      ┆ null     │\n",
       "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 424       ┆ 509       ┆ 4716      ┆ null     │\n",
       "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 66        ┆ 177       ┆ 1079      ┆ null     │\n",
       "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 143       ┆ 219       ┆ 1390      ┆ null     │\n",
       "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "└────────────┴───────────┴───────────┴──────────┴───┴───────────┴───────────┴───────────┴──────────┘"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>Month</th><th>State</th><th>Sex</th><th>Age Group</th><th>COVID-19 Deaths</th><th>Total Deaths</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;09/01/2023&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2023&quot;</td><td>&quot;9&quot;</td><td>&quot;Puerto Rico&quot;</td><td>&quot;Female&quot;</td><td>&quot;50-64 years&quot;</td><td>null</td><td>75</td><td>14</td><td>null</td><td>0</td><td>14</td><td>&quot;One or more da…</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;09/01/2023&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2023&quot;</td><td>&quot;9&quot;</td><td>&quot;Puerto Rico&quot;</td><td>&quot;Female&quot;</td><td>&quot;55-64 years&quot;</td><td>0</td><td>65</td><td>10</td><td>0</td><td>0</td><td>10</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;09/01/2023&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2023&quot;</td><td>&quot;9&quot;</td><td>&quot;Puerto Rico&quot;</td><td>&quot;Female&quot;</td><td>&quot;65-74 years&quot;</td><td>null</td><td>91</td><td>null</td><td>null</td><td>0</td><td>null</td><td>&quot;One or more da…</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;09/01/2023&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2023&quot;</td><td>&quot;9&quot;</td><td>&quot;Puerto Rico&quot;</td><td>&quot;Female&quot;</td><td>&quot;75-84 years&quot;</td><td>null</td><td>211</td><td>36</td><td>null</td><td>0</td><td>38</td><td>&quot;One or more da…</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;09/01/2023&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2023&quot;</td><td>&quot;9&quot;</td><td>&quot;Puerto Rico&quot;</td><td>&quot;Female&quot;</td><td>&quot;85 years and o…</td><td>null</td><td>265</td><td>42</td><td>null</td><td>null</td><td>44</td><td>&quot;One or more da…</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌───────────┬───────────┬───────────┬──────────┬───┬───────────┬───────────┬───────────┬───────────┐\n",
       "│ Data As   ┆ Start     ┆ End Date  ┆ Group    ┆ … ┆ Pneumonia ┆ Influenza ┆ Pneumonia ┆ Footnote  │\n",
       "│ Of        ┆ Date      ┆ ---       ┆ ---      ┆   ┆ and       ┆ Deaths    ┆ , Influen ┆ ---       │\n",
       "│ ---       ┆ ---       ┆ str       ┆ str      ┆   ┆ COVID-19  ┆ ---       ┆ za, or    ┆ str       │\n",
       "│ str       ┆ str       ┆           ┆          ┆   ┆ Deaths    ┆ i64       ┆ COVID-1…  ┆           │\n",
       "│           ┆           ┆           ┆          ┆   ┆ ---       ┆           ┆ ---       ┆           │\n",
       "│           ┆           ┆           ┆          ┆   ┆ i64       ┆           ┆ i64       ┆           │\n",
       "╞═══════════╪═══════════╪═══════════╪══════════╪═══╪═══════════╪═══════════╪═══════════╪═══════════╡\n",
       "│ 09/27/202 ┆ 09/01/202 ┆ 09/23/202 ┆ By Month ┆ … ┆ null      ┆ 0         ┆ 14        ┆ One or    │\n",
       "│ 3         ┆ 3         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆ more data │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ cells     │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ have      │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ coun…     │\n",
       "│ 09/27/202 ┆ 09/01/202 ┆ 09/23/202 ┆ By Month ┆ … ┆ 0         ┆ 0         ┆ 10        ┆ null      │\n",
       "│ 3         ┆ 3         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆           │\n",
       "│ 09/27/202 ┆ 09/01/202 ┆ 09/23/202 ┆ By Month ┆ … ┆ null      ┆ 0         ┆ null      ┆ One or    │\n",
       "│ 3         ┆ 3         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆ more data │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ cells     │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ have      │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ coun…     │\n",
       "│ 09/27/202 ┆ 09/01/202 ┆ 09/23/202 ┆ By Month ┆ … ┆ null      ┆ 0         ┆ 38        ┆ One or    │\n",
       "│ 3         ┆ 3         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆ more data │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ cells     │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ have      │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ coun…     │\n",
       "│ 09/27/202 ┆ 09/01/202 ┆ 09/23/202 ┆ By Month ┆ … ┆ null      ┆ null      ┆ 44        ┆ One or    │\n",
       "│ 3         ┆ 3         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆ more data │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ cells     │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ have      │\n",
       "│           ┆           ┆           ┆          ┆   ┆           ┆           ┆           ┆ coun…     │\n",
       "└───────────┴───────────┴───────────┴──────────┴───┴───────────┴───────────┴───────────┴───────────┘"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rows: 137700\n",
      "Columns: 16\n",
      "$ Data As Of                               <str> '09/27/2023', '09/27/2023', '09/27/2023'\n",
      "$ Start Date                               <str> '01/01/2020', '01/01/2020', '01/01/2020'\n",
      "$ End Date                                 <str> '09/23/2023', '09/23/2023', '09/23/2023'\n",
      "$ Group                                    <str> 'By Total', 'By Total', 'By Total'\n",
      "$ Year                                     <str> None, None, None\n",
      "$ Month                                    <str> None, None, None\n",
      "$ State                                    <str> 'United States', 'United States', 'United States'\n",
      "$ Sex                                      <str> 'All Sexes', 'All Sexes', 'All Sexes'\n",
      "$ Age Group                                <str> 'All Ages', 'Under 1 year', '0-17 years'\n",
      "$ COVID-19 Deaths                          <i64> 1146774, 519, 1696\n",
      "$ Total Deaths                             <i64> 12303399, 73213, 130970\n",
      "$ Pneumonia Deaths                         <i64> 1162844, 1056, 2961\n",
      "$ Pneumonia and COVID-19 Deaths            <i64> 569264, 95, 424\n",
      "$ Influenza Deaths                         <i64> 22229, 64, 509\n",
      "$ Pneumonia, Influenza, or COVID-19 Deaths <i64> 1760095, 1541, 4716\n",
      "$ Footnote                                 <str> None, None, None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "df.glimpse(max_items_per_column=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26.869342803955078"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.estimated_size('mb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (9, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>describe</th><th>COVID-19 Deaths</th><th>Total Deaths</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th></tr><tr><td>str</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>&quot;count&quot;</td><td>98270.0</td><td>118191.0</td><td>92836.0</td><td>100816.0</td><td>111012.0</td><td>93467.0</td></tr><tr><td>&quot;null_count&quot;</td><td>39430.0</td><td>19509.0</td><td>44864.0</td><td>36884.0</td><td>26688.0</td><td>44233.0</td></tr><tr><td>&quot;mean&quot;</td><td>313.586547</td><td>2841.952585</td><td>336.597085</td><td>152.513411</td><td>5.002468</td><td>505.491778</td></tr><tr><td>&quot;std&quot;</td><td>5992.341375</td><td>56201.384331</td><td>6126.573599</td><td>2980.886938</td><td>110.606691</td><td>9256.951591</td></tr><tr><td>&quot;min&quot;</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td></tr><tr><td>&quot;25%&quot;</td><td>0.0</td><td>43.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td></tr><tr><td>&quot;50%&quot;</td><td>0.0</td><td>153.0</td><td>18.0</td><td>0.0</td><td>0.0</td><td>25.0</td></tr><tr><td>&quot;75%&quot;</td><td>50.0</td><td>657.0</td><td>74.0</td><td>21.0</td><td>0.0</td><td>107.0</td></tr><tr><td>&quot;max&quot;</td><td>1.146774e6</td><td>1.2303399e7</td><td>1.162844e6</td><td>569264.0</td><td>22229.0</td><td>1.760095e6</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (9, 7)\n",
       "┌────────────┬──────────────┬──────────────┬─────────────┬─────────────┬─────────────┬─────────────┐\n",
       "│ describe   ┆ COVID-19     ┆ Total Deaths ┆ Pneumonia   ┆ Pneumonia   ┆ Influenza   ┆ Pneumonia,  │\n",
       "│ ---        ┆ Deaths       ┆ ---          ┆ Deaths      ┆ and         ┆ Deaths      ┆ Influenza,  │\n",
       "│ str        ┆ ---          ┆ f64          ┆ ---         ┆ COVID-19    ┆ ---         ┆ or COVID-1… │\n",
       "│            ┆ f64          ┆              ┆ f64         ┆ Deaths      ┆ f64         ┆ ---         │\n",
       "│            ┆              ┆              ┆             ┆ ---         ┆             ┆ f64         │\n",
       "│            ┆              ┆              ┆             ┆ f64         ┆             ┆             │\n",
       "╞════════════╪══════════════╪══════════════╪═════════════╪═════════════╪═════════════╪═════════════╡\n",
       "│ count      ┆ 98270.0      ┆ 118191.0     ┆ 92836.0     ┆ 100816.0    ┆ 111012.0    ┆ 93467.0     │\n",
       "│ null_count ┆ 39430.0      ┆ 19509.0      ┆ 44864.0     ┆ 36884.0     ┆ 26688.0     ┆ 44233.0     │\n",
       "│ mean       ┆ 313.586547   ┆ 2841.952585  ┆ 336.597085  ┆ 152.513411  ┆ 5.002468    ┆ 505.491778  │\n",
       "│ std        ┆ 5992.341375  ┆ 56201.384331 ┆ 6126.573599 ┆ 2980.886938 ┆ 110.606691  ┆ 9256.951591 │\n",
       "│ min        ┆ 0.0          ┆ 0.0          ┆ 0.0         ┆ 0.0         ┆ 0.0         ┆ 0.0         │\n",
       "│ 25%        ┆ 0.0          ┆ 43.0         ┆ 0.0         ┆ 0.0         ┆ 0.0         ┆ 0.0         │\n",
       "│ 50%        ┆ 0.0          ┆ 153.0        ┆ 18.0        ┆ 0.0         ┆ 0.0         ┆ 25.0        │\n",
       "│ 75%        ┆ 50.0         ┆ 657.0        ┆ 74.0        ┆ 21.0        ┆ 0.0         ┆ 107.0       │\n",
       "│ max        ┆ 1.146774e6   ┆ 1.2303399e7  ┆ 1.162844e6  ┆ 569264.0    ┆ 22229.0     ┆ 1.760095e6  │\n",
       "└────────────┴──────────────┴──────────────┴─────────────┴─────────────┴─────────────┴─────────────┘"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars.selectors as cs\n",
    "df.select(cs.numeric()).describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (1, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>Month</th><th>State</th><th>Sex</th><th>Age Group</th><th>COVID-19 Deaths</th><th>Total Deaths</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td></tr></thead><tbody><tr><td>0</td><td>0</td><td>0</td><td>0</td><td>2754</td><td>13770</td><td>0</td><td>0</td><td>0</td><td>39430</td><td>19509</td><td>44864</td><td>36884</td><td>26688</td><td>44233</td><td>39804</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (1, 16)\n",
       "┌────────────┬────────────┬──────────┬───────┬───┬────────────┬────────────┬────────────┬──────────┐\n",
       "│ Data As Of ┆ Start Date ┆ End Date ┆ Group ┆ … ┆ Pneumonia  ┆ Influenza  ┆ Pneumonia, ┆ Footnote │\n",
       "│ ---        ┆ ---        ┆ ---      ┆ ---   ┆   ┆ and        ┆ Deaths     ┆ Influenza, ┆ ---      │\n",
       "│ u32        ┆ u32        ┆ u32      ┆ u32   ┆   ┆ COVID-19   ┆ ---        ┆ or         ┆ u32      │\n",
       "│            ┆            ┆          ┆       ┆   ┆ Deaths     ┆ u32        ┆ COVID-1…   ┆          │\n",
       "│            ┆            ┆          ┆       ┆   ┆ ---        ┆            ┆ ---        ┆          │\n",
       "│            ┆            ┆          ┆       ┆   ┆ u32        ┆            ┆ u32        ┆          │\n",
       "╞════════════╪════════════╪══════════╪═══════╪═══╪════════════╪════════════╪════════════╪══════════╡\n",
       "│ 0          ┆ 0          ┆ 0        ┆ 0     ┆ … ┆ 36884      ┆ 26688      ┆ 44233      ┆ 39804    │\n",
       "└────────────┴────────────┴──────────┴───────┴───┴────────────┴────────────┴────────────┴──────────┘"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.null_count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There is more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape: (5, 16)\n",
      "┌────────────┬───────────┬───────────┬──────────┬───┬───────────┬───────────┬───────────┬──────────┐\n",
      "│ Data As Of ┆ Start     ┆ End Date  ┆ Group    ┆ … ┆ Pneumonia ┆ Influenza ┆ Pneumonia ┆ Footnote │\n",
      "│ ---        ┆ Date      ┆ ---       ┆ ---      ┆   ┆ and       ┆ Deaths    ┆ , Influen ┆ ---      │\n",
      "│ str        ┆ ---       ┆ str       ┆ str      ┆   ┆ COVID-19  ┆ ---       ┆ za, or    ┆ str      │\n",
      "│            ┆ str       ┆           ┆          ┆   ┆ Deaths    ┆ i64       ┆ COVID-1…  ┆          │\n",
      "│            ┆           ┆           ┆          ┆   ┆ ---       ┆           ┆ ---       ┆          │\n",
      "│            ┆           ┆           ┆          ┆   ┆ i64       ┆           ┆ i64       ┆          │\n",
      "╞════════════╪═══════════╪═══════════╪══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡\n",
      "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 569264    ┆ 22229     ┆ 1760095   ┆ null     │\n",
      "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
      "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 95        ┆ 64        ┆ 1541      ┆ null     │\n",
      "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
      "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 424       ┆ 509       ┆ 4716      ┆ null     │\n",
      "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
      "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 66        ┆ 177       ┆ 1079      ┆ null     │\n",
      "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
      "│ 09/27/2023 ┆ 01/01/202 ┆ 09/23/202 ┆ By Total ┆ … ┆ 143       ┆ 219       ┆ 1390      ┆ null     │\n",
      "│            ┆ 0         ┆ 3         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
      "└────────────┴───────────┴───────────┴──────────┴───┴───────────┴───────────┴───────────┴──────────┘\n"
     ]
    }
   ],
   "source": [
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape: (2, 16)\n",
      "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
      "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footn │\n",
      "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ ote   │\n",
      "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ str  ┆ str   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---   │\n",
      "│ str    ┆ str    ┆ str    ┆        ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ str   │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆ 19     ┆ i64    ┆ or COV ┆       │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆       │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆       │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ i64    ┆        ┆ i64    ┆       │\n",
      "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
      "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 116284 ┆ 569264 ┆ 22229  ┆ 176009 ┆ null  │\n",
      "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆ 4      ┆        ┆        ┆ 5      ┆       │\n",
      "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 1056   ┆ 95     ┆ 64     ┆ 1541   ┆ null  │\n",
      "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
      "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘\n"
     ]
    }
   ],
   "source": [
    "with pl.Config() as config:\n",
    "    config.set_tbl_cols(11)\n",
    "    print(df.head(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape: (2, 16)\n",
      "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
      "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footn │\n",
      "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ ote   │\n",
      "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ str  ┆ str   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---   │\n",
      "│ str    ┆ str    ┆ str    ┆        ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ str   │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆ 19     ┆ i64    ┆ or COV ┆       │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆       │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆       │\n",
      "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ i64    ┆        ┆ i64    ┆       │\n",
      "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
      "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 116284 ┆ 569264 ┆ 22229  ┆ 176009 ┆ null  │\n",
      "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆ 4      ┆        ┆        ┆ 5      ┆       │\n",
      "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 1056   ┆ 95     ┆ 64     ┆ 1541   ┆ null  │\n",
      "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
      "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘\n"
     ]
    }
   ],
   "source": [
    "pl.Config.set_tbl_cols(11)\n",
    "print(df.head(2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Casting data types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>&hellip;</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1162844</td><td>569264</td><td>22229</td><td>1760095</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1056</td><td>95</td><td>64</td><td>1541</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>2961</td><td>424</td><td>509</td><td>4716</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>692</td><td>66</td><td>177</td><td>1079</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>818</td><td>143</td><td>219</td><td>1390</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footn │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ ote   │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ str  ┆ str   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---   │\n",
       "│ str    ┆ str    ┆ str    ┆        ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ str   │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆ 19     ┆ i64    ┆ or COV ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ i64    ┆        ┆ i64    ┆       │\n",
       "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 116284 ┆ 569264 ┆ 22229  ┆ 176009 ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆ 4      ┆        ┆        ┆ 5      ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 1056   ┆ 95     ┆ 64     ┆ 1541   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 2961   ┆ 424    ┆ 509    ┆ 4716   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 692    ┆ 66     ┆ 177    ┆ 1079   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 818    ┆ 143    ┆ 219    ┆ 1390   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/covid_19_deaths.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 17)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th><th>End Date 2</th></tr><tr><td>date</td><td>date</td><td>date</td><td>str</td><td>i64</td><td>&hellip;</td><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>date</td></tr></thead><tbody><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>569264</td><td>22229</td><td>1760095</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>95</td><td>64</td><td>1541</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>424</td><td>509</td><td>4716</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>66</td><td>177</td><td>1079</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>143</td><td>219</td><td>1390</td><td>null</td><td>2023-09-23</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 17)\n",
       "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footno ┆ End   │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nza    ┆ nia,   ┆ te     ┆ Date  │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ i64  ┆ str   ┆   ┆ and    ┆ Deaths ┆ Influe ┆ ---    ┆ 2     │\n",
       "│ date   ┆ date   ┆ date   ┆        ┆      ┆       ┆   ┆ COVID- ┆ ---    ┆ nza,   ┆ str    ┆ ---   │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ 19     ┆ i64    ┆ or COV ┆        ┆ date  │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ Deaths ┆        ┆ ID-1…  ┆        ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ ---    ┆        ┆ ---    ┆        ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆        ┆ i64    ┆        ┆       │\n",
       "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 569264 ┆ 22229  ┆ 176009 ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆ 5      ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 95     ┆ 64     ┆ 1541   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 424    ┆ 509    ┆ 4716   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 66     ┆ 177    ┆ 1079   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 143    ┆ 219    ┆ 1390   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(\n",
    "        pl.col('Data As Of').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('Start Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('End Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('End Date').str.to_date('%m/%d/%Y').alias('End Date 2'),\n",
    "        pl.col('Year').cast(pl.Int64)\n",
    ").head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "updated_df = (\n",
    "    df.with_columns(\n",
    "        pl.col('Data As Of').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('Start Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('End Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('End Date').str.to_date('%m/%d/%Y').alias('End Date 2'),\n",
    "        pl.col('Year').cast(pl.Int64)\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 17)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th><th>End Date 2</th></tr><tr><td>date</td><td>date</td><td>date</td><td>str</td><td>i64</td><td>&hellip;</td><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>date</td></tr></thead><tbody><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>569264</td><td>22229</td><td>1760095</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>95</td><td>64</td><td>1541</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>424</td><td>509</td><td>4716</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>66</td><td>177</td><td>1079</td><td>null</td><td>2023-09-23</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2023-09-23</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>143</td><td>219</td><td>1390</td><td>null</td><td>2023-09-23</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 17)\n",
       "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footno ┆ End   │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nza    ┆ nia,   ┆ te     ┆ Date  │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ i64  ┆ str   ┆   ┆ and    ┆ Deaths ┆ Influe ┆ ---    ┆ 2     │\n",
       "│ date   ┆ date   ┆ date   ┆        ┆      ┆       ┆   ┆ COVID- ┆ ---    ┆ nza,   ┆ str    ┆ ---   │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ 19     ┆ i64    ┆ or COV ┆        ┆ date  │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ Deaths ┆        ┆ ID-1…  ┆        ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ ---    ┆        ┆ ---    ┆        ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆        ┆ i64    ┆        ┆       │\n",
       "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 569264 ┆ 22229  ┆ 176009 ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆ 5      ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 95     ┆ 64     ┆ 1541   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 424    ┆ 509    ┆ 4716   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 66     ┆ 177    ┆ 1079   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "│ 2023-0 ┆ 2020-0 ┆ 2023-0 ┆ By     ┆ null ┆ null  ┆ … ┆ 143    ┆ 219    ┆ 1390   ┆ null   ┆ 2023- │\n",
       "│ 9-27   ┆ 1-01   ┆ 9-23   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆ 09-23 │\n",
       "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf = pl.scan_csv('../data/covid_19_deaths.csv')\n",
    "lf.with_columns(\n",
    "        pl.col('Data As Of').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('Start Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('End Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('End Date').str.to_date('%m/%d/%Y').alias('End Date 2'),\n",
    "        pl.col('Year').cast(pl.Int64)\n",
    ").collect().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finding and removing duplicates values "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>&hellip;</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1162844</td><td>569264</td><td>22229</td><td>1760095</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1056</td><td>95</td><td>64</td><td>1541</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>2961</td><td>424</td><td>509</td><td>4716</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>692</td><td>66</td><td>177</td><td>1079</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>818</td><td>143</td><td>219</td><td>1390</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footn │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ ote   │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ str  ┆ str   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---   │\n",
       "│ str    ┆ str    ┆ str    ┆        ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ str   │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆ 19     ┆ i64    ┆ or COV ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ i64    ┆        ┆ i64    ┆       │\n",
       "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 116284 ┆ 569264 ┆ 22229  ┆ 176009 ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆ 4      ┆        ┆        ┆ 5      ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 1056   ┆ 95     ┆ 64     ┆ 1541   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 2961   ┆ 424    ┆ 509    ┆ 4716   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 692    ┆ 66     ┆ 177    ┆ 1079   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 818    ┆ 143    ┆ 219    ┆ 1390   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/covid_19_deaths.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(137700, 16)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.is_duplicated().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "137700"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.is_unique().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "137700"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.n_unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (1, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>&hellip;</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td></tr></thead><tbody><tr><td>1</td><td>45</td><td>45</td><td>3</td><td>5</td><td>&hellip;</td><td>3556</td><td>2533</td><td>493</td><td>4264</td><td>2</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (1, 16)\n",
       "┌────────┬────────┬────────┬───────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬────────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footno │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---   ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ te     │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ u32   ┆ u32  ┆ u32   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---    │\n",
       "│ u32    ┆ u32    ┆ u32    ┆       ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ u32    │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆ u32    ┆ 19     ┆ u32    ┆ or COV ┆        │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆        │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆        │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆        ┆ u32    ┆        ┆ u32    ┆        │\n",
       "╞════════╪════════╪════════╪═══════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪════════╡\n",
       "│ 1      ┆ 45     ┆ 45     ┆ 3     ┆ 5    ┆ 13    ┆ … ┆ 3556   ┆ 2533   ┆ 493    ┆ 4264   ┆ 2      │\n",
       "└────────┴────────┴────────┴───────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴────────┘"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(pl.all().n_unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.n_unique(subset=['Start Date', 'End Date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>Month</th><th>State</th><th>Sex</th><th>Age Group</th><th>COVID-19 Deaths</th><th>Total Deaths</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;10/01/2020&quot;</td><td>&quot;10/31/2020&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2020&quot;</td><td>&quot;10&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>24930</td><td>273912</td><td>24327</td><td>11734</td><td>69</td><td>37570</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;02/01/2021&quot;</td><td>&quot;02/28/2021&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2021&quot;</td><td>&quot;2&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>48570</td><td>282558</td><td>38081</td><td>26128</td><td>90</td><td>60580</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;07/01/2022&quot;</td><td>&quot;07/31/2022&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2022&quot;</td><td>&quot;7&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>13393</td><td>260978</td><td>16405</td><td>4572</td><td>109</td><td>25315</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;07/01/2020&quot;</td><td>&quot;07/31/2020&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2020&quot;</td><td>&quot;7&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>31135</td><td>279008</td><td>27121</td><td>14903</td><td>50</td><td>43385</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2021&quot;</td><td>&quot;01/31/2021&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2021&quot;</td><td>&quot;1&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>105565</td><td>373641</td><td>69849</td><td>55416</td><td>144</td><td>120079</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────────┬───────────┬───────────┬──────────┬───┬───────────┬───────────┬───────────┬──────────┐\n",
       "│ Data As Of ┆ Start     ┆ End Date  ┆ Group    ┆ … ┆ Pneumonia ┆ Influenza ┆ Pneumonia ┆ Footnote │\n",
       "│ ---        ┆ Date      ┆ ---       ┆ ---      ┆   ┆ and       ┆ Deaths    ┆ , Influen ┆ ---      │\n",
       "│ str        ┆ ---       ┆ str       ┆ str      ┆   ┆ COVID-19  ┆ ---       ┆ za, or    ┆ str      │\n",
       "│            ┆ str       ┆           ┆          ┆   ┆ Deaths    ┆ i64       ┆ COVID…    ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ ---       ┆           ┆ ---       ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ i64       ┆           ┆ i64       ┆          │\n",
       "╞════════════╪═══════════╪═══════════╪══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡\n",
       "│ 09/27/2023 ┆ 10/01/202 ┆ 10/31/202 ┆ By Month ┆ … ┆ 11734     ┆ 69        ┆ 37570     ┆ null     │\n",
       "│            ┆ 0         ┆ 0         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 02/01/202 ┆ 02/28/202 ┆ By Month ┆ … ┆ 26128     ┆ 90        ┆ 60580     ┆ null     │\n",
       "│            ┆ 1         ┆ 1         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 07/01/202 ┆ 07/31/202 ┆ By Month ┆ … ┆ 4572      ┆ 109       ┆ 25315     ┆ null     │\n",
       "│            ┆ 2         ┆ 2         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 07/01/202 ┆ 07/31/202 ┆ By Month ┆ … ┆ 14903     ┆ 50        ┆ 43385     ┆ null     │\n",
       "│            ┆ 0         ┆ 0         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 01/31/202 ┆ By Month ┆ … ┆ 55416     ┆ 144       ┆ 120079    ┆ null     │\n",
       "│            ┆ 1         ┆ 1         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "└────────────┴───────────┴───────────┴──────────┴───┴───────────┴───────────┴───────────┴──────────┘"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    df.unique(subset=['Start Date', 'End Date'], keep='first')\n",
    "    .head()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3940"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rows_to_keep = df.select(['Year', 'COVID-19 Deaths']).is_unique()\n",
    "rows_to_keep.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3940, 16)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter(rows_to_keep).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>&hellip;</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1162844</td><td>569264</td><td>22229</td><td>1760095</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1056</td><td>95</td><td>64</td><td>1541</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>2961</td><td>424</td><td>509</td><td>4716</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>692</td><td>66</td><td>177</td><td>1079</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>818</td><td>143</td><td>219</td><td>1390</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footn │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ ote   │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ str  ┆ str   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---   │\n",
       "│ str    ┆ str    ┆ str    ┆        ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ str   │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆ 19     ┆ i64    ┆ or COV ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ i64    ┆        ┆ i64    ┆       │\n",
       "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 116284 ┆ 569264 ┆ 22229  ┆ 176009 ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆ 4      ┆        ┆        ┆ 5      ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 1056   ┆ 95     ┆ 64     ┆ 1541   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 2961   ┆ 424    ┆ 509    ┆ 4716   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 692    ┆ 66     ┆ 177    ┆ 1079   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 818    ┆ 143    ┆ 219    ┆ 1390   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter(rows_to_keep).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There is more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (1, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>&hellip;</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td><td>u32</td></tr></thead><tbody><tr><td>1</td><td>45</td><td>44</td><td>3</td><td>5</td><td>&hellip;</td><td>3587</td><td>2536</td><td>496</td><td>4263</td><td>2</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (1, 16)\n",
       "┌────────┬────────┬────────┬───────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬────────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footno │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---   ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ te     │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ u32   ┆ u32  ┆ u32   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---    │\n",
       "│ u32    ┆ u32    ┆ u32    ┆       ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ u32    │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆ u32    ┆ 19     ┆ u32    ┆ or COV ┆        │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆        │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆        │\n",
       "│        ┆        ┆        ┆       ┆      ┆       ┆   ┆        ┆ u32    ┆        ┆ u32    ┆        │\n",
       "╞════════╪════════╪════════╪═══════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪════════╡\n",
       "│ 1      ┆ 45     ┆ 44     ┆ 3     ┆ 5    ┆ 13    ┆ … ┆ 3587   ┆ 2536   ┆ 496    ┆ 4263   ┆ 2      │\n",
       "└────────┴────────┴────────┴───────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴────────┘"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(pl.all().approx_n_unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Masking sensitive data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>&hellip;</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>&hellip;</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1162844</td><td>569264</td><td>22229</td><td>1760095</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>1056</td><td>95</td><td>64</td><td>1541</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>2961</td><td>424</td><td>509</td><td>4716</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>692</td><td>66</td><td>177</td><td>1079</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;09/23/2023&quot;</td><td>&quot;By Total&quot;</td><td>null</td><td>&hellip;</td><td>818</td><td>143</td><td>219</td><td>1390</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────┬────────┬────────┬────────┬──────┬───────┬───┬────────┬────────┬────────┬────────┬───────┐\n",
       "│ Data   ┆ Start  ┆ End    ┆ Group  ┆ Year ┆ Month ┆ … ┆ Pneumo ┆ Pneumo ┆ Influe ┆ Pneumo ┆ Footn │\n",
       "│ As Of  ┆ Date   ┆ Date   ┆ ---    ┆ ---  ┆ ---   ┆   ┆ nia    ┆ nia    ┆ nza    ┆ nia,   ┆ ote   │\n",
       "│ ---    ┆ ---    ┆ ---    ┆ str    ┆ str  ┆ str   ┆   ┆ Deaths ┆ and    ┆ Deaths ┆ Influe ┆ ---   │\n",
       "│ str    ┆ str    ┆ str    ┆        ┆      ┆       ┆   ┆ ---    ┆ COVID- ┆ ---    ┆ nza,   ┆ str   │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆ i64    ┆ 19     ┆ i64    ┆ or COV ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ Deaths ┆        ┆ ID-1…  ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ ---    ┆        ┆ ---    ┆       │\n",
       "│        ┆        ┆        ┆        ┆      ┆       ┆   ┆        ┆ i64    ┆        ┆ i64    ┆       │\n",
       "╞════════╪════════╪════════╪════════╪══════╪═══════╪═══╪════════╪════════╪════════╪════════╪═══════╡\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 116284 ┆ 569264 ┆ 22229  ┆ 176009 ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆ 4      ┆        ┆        ┆ 5      ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 1056   ┆ 95     ┆ 64     ┆ 1541   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 2961   ┆ 424    ┆ 509    ┆ 4716   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 692    ┆ 66     ┆ 177    ┆ 1079   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "│ 09/27/ ┆ 01/01/ ┆ 09/23/ ┆ By     ┆ null ┆ null  ┆ … ┆ 818    ┆ 143    ┆ 219    ┆ 1390   ┆ null  │\n",
       "│ 2023   ┆ 2020   ┆ 2023   ┆ Total  ┆      ┆       ┆   ┆        ┆        ┆        ┆        ┆       │\n",
       "└────────┴────────┴────────┴────────┴──────┴───────┴───┴────────┴────────┴────────┴────────┴───────┘"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/covid_19_deaths.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "def get_random_nums(num_list, length):\n",
    "    random_nums = ''.join(str(n) for n in random.sample(num_list, length))\n",
    "    return random_nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>SSN</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;078-07-3074&quot;</td></tr><tr><td>&quot;180-64-9315&quot;</td></tr><tr><td>&quot;149-56-0846&quot;</td></tr><tr><td>&quot;153-01-5036&quot;</td></tr><tr><td>&quot;976-26-7940&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 1)\n",
       "┌─────────────┐\n",
       "│ SSN         │\n",
       "│ ---         │\n",
       "│ str         │\n",
       "╞═════════════╡\n",
       "│ 078-07-3074 │\n",
       "│ 180-64-9315 │\n",
       "│ 149-56-0846 │\n",
       "│ 153-01-5036 │\n",
       "│ 976-26-7940 │\n",
       "└─────────────┘"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fake_ssns = []\n",
    "nums = [n for n in range(10)]\n",
    "\n",
    "for i in range(df.height):\n",
    "    part_1 = get_random_nums(nums, 3)\n",
    "    part_2 = get_random_nums(nums, 2)\n",
    "    part_3 = get_random_nums(nums, 4)\n",
    "    fake_ssn = f'{part_1}-{part_2}-{part_3}'\n",
    "    fake_ssns.append(fake_ssn)\n",
    "\n",
    "random.seed(10)\n",
    "fake_ssns_df = pl.DataFrame({'SSN': fake_ssns})\n",
    "fake_ssns_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pl.concat([df, fake_ssns_df], how='horizontal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>SSN Masked</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;XXX-XX-XX74&quot;</td></tr><tr><td>&quot;XXX-XX-XX15&quot;</td></tr><tr><td>&quot;XXX-XX-XX46&quot;</td></tr><tr><td>&quot;XXX-XX-XX36&quot;</td></tr><tr><td>&quot;XXX-XX-XX40&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 1)\n",
       "┌─────────────┐\n",
       "│ SSN Masked  │\n",
       "│ ---         │\n",
       "│ str         │\n",
       "╞═════════════╡\n",
       "│ XXX-XX-XX74 │\n",
       "│ XXX-XX-XX15 │\n",
       "│ XXX-XX-XX46 │\n",
       "│ XXX-XX-XX36 │\n",
       "│ XXX-XX-XX40 │\n",
       "└─────────────┘"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(\n",
    "    ('XXX-XX-XX' + pl.col('SSN').str.slice(9, 2)).alias('SSN Masked')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>SSN Masked</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;XXX-XX-XX74&quot;</td></tr><tr><td>&quot;XXX-XX-XX15&quot;</td></tr><tr><td>&quot;XXX-XX-XX46&quot;</td></tr><tr><td>&quot;XXX-XX-XX36&quot;</td></tr><tr><td>&quot;XXX-XX-XX40&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 1)\n",
       "┌─────────────┐\n",
       "│ SSN Masked  │\n",
       "│ ---         │\n",
       "│ str         │\n",
       "╞═════════════╡\n",
       "│ XXX-XX-XX74 │\n",
       "│ XXX-XX-XX15 │\n",
       "│ XXX-XX-XX46 │\n",
       "│ XXX-XX-XX36 │\n",
       "│ XXX-XX-XX40 │\n",
       "└─────────────┘"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(\n",
    "    ('XXX-XX-XX' + pl.col('SSN').str.slice(9, 2)).alias('SSN Masked'),\n",
    "    \n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>SSN</th></tr><tr><td>u64</td></tr></thead><tbody><tr><td>16688716547289931698</td></tr><tr><td>15739169009208351387</td></tr><tr><td>13236024879537598761</td></tr><tr><td>16141549672241121801</td></tr><tr><td>8396280402906047174</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 1)\n",
       "┌──────────────────────┐\n",
       "│ SSN                  │\n",
       "│ ---                  │\n",
       "│ u64                  │\n",
       "╞══════════════════════╡\n",
       "│ 16688716547289931698 │\n",
       "│ 15739169009208351387 │\n",
       "│ 13236024879537598761 │\n",
       "│ 16141549672241121801 │\n",
       "│ 8396280402906047174  │\n",
       "└──────────────────────┘"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(\n",
    "    pl.col('SSN').hash()\n",
    ").head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualizing data using Plotly"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl\n",
    "import plotly.express as px"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>Month</th><th>State</th><th>Sex</th><th>Age Group</th><th>COVID-19 Deaths</th><th>Total Deaths</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;01/31/2020&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2020&quot;</td><td>&quot;1&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>6</td><td>264677</td><td>17909</td><td>3</td><td>2125</td><td>20037</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;01/31/2020&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2020&quot;</td><td>&quot;1&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;0-17 years&quot;</td><td>0</td><td>2966</td><td>90</td><td>0</td><td>63</td><td>153</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;01/31/2020&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2020&quot;</td><td>&quot;1&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;18-29 years&quot;</td><td>0</td><td>4426</td><td>114</td><td>0</td><td>54</td><td>168</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;01/31/2020&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2020&quot;</td><td>&quot;1&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;30-39 years&quot;</td><td>0</td><td>6475</td><td>246</td><td>0</td><td>112</td><td>358</td><td>null</td></tr><tr><td>&quot;09/27/2023&quot;</td><td>&quot;01/01/2020&quot;</td><td>&quot;01/31/2020&quot;</td><td>&quot;By Month&quot;</td><td>&quot;2020&quot;</td><td>&quot;1&quot;</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;40-49 years&quot;</td><td>0</td><td>9792</td><td>485</td><td>0</td><td>151</td><td>636</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────────┬───────────┬───────────┬──────────┬───┬───────────┬───────────┬───────────┬──────────┐\n",
       "│ Data As Of ┆ Start     ┆ End Date  ┆ Group    ┆ … ┆ Pneumonia ┆ Influenza ┆ Pneumonia ┆ Footnote │\n",
       "│ ---        ┆ Date      ┆ ---       ┆ ---      ┆   ┆ and       ┆ Deaths    ┆ , Influen ┆ ---      │\n",
       "│ str        ┆ ---       ┆ str       ┆ str      ┆   ┆ COVID-19  ┆ ---       ┆ za, or    ┆ str      │\n",
       "│            ┆ str       ┆           ┆          ┆   ┆ Deaths    ┆ i64       ┆ COVID-1…  ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ ---       ┆           ┆ ---       ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ i64       ┆           ┆ i64       ┆          │\n",
       "╞════════════╪═══════════╪═══════════╪══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 01/31/202 ┆ By Month ┆ … ┆ 3         ┆ 2125      ┆ 20037     ┆ null     │\n",
       "│            ┆ 0         ┆ 0         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 01/31/202 ┆ By Month ┆ … ┆ 0         ┆ 63        ┆ 153       ┆ null     │\n",
       "│            ┆ 0         ┆ 0         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 01/31/202 ┆ By Month ┆ … ┆ 0         ┆ 54        ┆ 168       ┆ null     │\n",
       "│            ┆ 0         ┆ 0         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 01/31/202 ┆ By Month ┆ … ┆ 0         ┆ 112       ┆ 358       ┆ null     │\n",
       "│            ┆ 0         ┆ 0         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 09/27/2023 ┆ 01/01/202 ┆ 01/31/202 ┆ By Month ┆ … ┆ 0         ┆ 151       ┆ 636       ┆ null     │\n",
       "│            ┆ 0         ┆ 0         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "└────────────┴───────────┴───────────┴──────────┴───┴───────────┴───────────┴───────────┴──────────┘"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_groups = ['0-17 years', '18-29 years', '30-39 years', '40-49 years', '50-64 years', '65-74 years', '75-84 years', '85 years and over', 'All Ages']\n",
    "\n",
    "df = (\n",
    "    pl.read_csv('../data/covid_19_deaths.csv')\n",
    "    .filter(\n",
    "        pl.col('Month').is_not_null(),\n",
    "        pl.col('Age Group').is_in(age_groups),\n",
    "    )\n",
    ")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 16)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Data As Of</th><th>Start Date</th><th>End Date</th><th>Group</th><th>Year</th><th>Month</th><th>State</th><th>Sex</th><th>Age Group</th><th>COVID-19 Deaths</th><th>Total Deaths</th><th>Pneumonia Deaths</th><th>Pneumonia and COVID-19 Deaths</th><th>Influenza Deaths</th><th>Pneumonia, Influenza, or COVID-19 Deaths</th><th>Footnote</th></tr><tr><td>date</td><td>date</td><td>date</td><td>str</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>str</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>2023-09-27</td><td>2020-01-01</td><td>2020-01-31</td><td>&quot;By Month&quot;</td><td>2020</td><td>1</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;All Ages&quot;</td><td>6</td><td>264677</td><td>17909</td><td>3</td><td>2125</td><td>20037</td><td>null</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2020-01-31</td><td>&quot;By Month&quot;</td><td>2020</td><td>1</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;0-17 years&quot;</td><td>0</td><td>2966</td><td>90</td><td>0</td><td>63</td><td>153</td><td>null</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2020-01-31</td><td>&quot;By Month&quot;</td><td>2020</td><td>1</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;18-29 years&quot;</td><td>0</td><td>4426</td><td>114</td><td>0</td><td>54</td><td>168</td><td>null</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2020-01-31</td><td>&quot;By Month&quot;</td><td>2020</td><td>1</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;30-39 years&quot;</td><td>0</td><td>6475</td><td>246</td><td>0</td><td>112</td><td>358</td><td>null</td></tr><tr><td>2023-09-27</td><td>2020-01-01</td><td>2020-01-31</td><td>&quot;By Month&quot;</td><td>2020</td><td>1</td><td>&quot;United States&quot;</td><td>&quot;All Sexes&quot;</td><td>&quot;40-49 years&quot;</td><td>0</td><td>9792</td><td>485</td><td>0</td><td>151</td><td>636</td><td>null</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 16)\n",
       "┌────────────┬───────────┬───────────┬──────────┬───┬───────────┬───────────┬───────────┬──────────┐\n",
       "│ Data As Of ┆ Start     ┆ End Date  ┆ Group    ┆ … ┆ Pneumonia ┆ Influenza ┆ Pneumonia ┆ Footnote │\n",
       "│ ---        ┆ Date      ┆ ---       ┆ ---      ┆   ┆ and       ┆ Deaths    ┆ , Influen ┆ ---      │\n",
       "│ date       ┆ ---       ┆ date      ┆ str      ┆   ┆ COVID-19  ┆ ---       ┆ za, or    ┆ str      │\n",
       "│            ┆ date      ┆           ┆          ┆   ┆ Deaths    ┆ i64       ┆ COVID-1…  ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ ---       ┆           ┆ ---       ┆          │\n",
       "│            ┆           ┆           ┆          ┆   ┆ i64       ┆           ┆ i64       ┆          │\n",
       "╞════════════╪═══════════╪═══════════╪══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡\n",
       "│ 2023-09-27 ┆ 2020-01-0 ┆ 2020-01-3 ┆ By Month ┆ … ┆ 3         ┆ 2125      ┆ 20037     ┆ null     │\n",
       "│            ┆ 1         ┆ 1         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 2023-09-27 ┆ 2020-01-0 ┆ 2020-01-3 ┆ By Month ┆ … ┆ 0         ┆ 63        ┆ 153       ┆ null     │\n",
       "│            ┆ 1         ┆ 1         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 2023-09-27 ┆ 2020-01-0 ┆ 2020-01-3 ┆ By Month ┆ … ┆ 0         ┆ 54        ┆ 168       ┆ null     │\n",
       "│            ┆ 1         ┆ 1         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 2023-09-27 ┆ 2020-01-0 ┆ 2020-01-3 ┆ By Month ┆ … ┆ 0         ┆ 112       ┆ 358       ┆ null     │\n",
       "│            ┆ 1         ┆ 1         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "│ 2023-09-27 ┆ 2020-01-0 ┆ 2020-01-3 ┆ By Month ┆ … ┆ 0         ┆ 151       ┆ 636       ┆ null     │\n",
       "│            ┆ 1         ┆ 1         ┆          ┆   ┆           ┆           ┆           ┆          │\n",
       "└────────────┴───────────┴───────────┴──────────┴───┴───────────┴───────────┴───────────┴──────────┘"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = (\n",
    "    df.\n",
    "    with_columns(\n",
    "        pl.col('Data As Of').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('Start Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('End Date').str.strptime(pl.Date, '%m/%d/%Y'),\n",
    "        pl.col('Year').cast(pl.Int64),\n",
    "        pl.col('Month').cast(pl.Int64)\n",
    "    )\n",
    ") \n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "alignmentgroup": "True",
         "hovertemplate": "Age Group=%{x}<br>COVID-19 Deaths=%{y}<extra></extra>",
         "legendgroup": "",
         "marker": {
          "color": "#636efa",
          "pattern": {
           "shape": ""
          }
         },
         "name": "",
         "offsetgroup": "",
         "orientation": "v",
         "showlegend": false,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "85 years and over",
          "75-84 years",
          "65-74 years",
          "50-64 years",
          "40-49 years",
          "30-39 years",
          "18-29 years",
          "0-17 years"
         ],
         "xaxis": "x",
         "y": [
          20391,
          15686,
          9264,
          4762,
          835,
          426,
          183,
          137
         ],
         "yaxis": "y"
        }
       ],
       "layout": {
        "barmode": "relative",
        "legend": {
         "tracegroupgap": 0
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "COVID Deaths 2023 by Age Group - As of 9/27/23"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ],
         "title": {}
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "COVID-19 Deaths"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "covid_deaths_by_age = (\n",
    "    df\n",
    "    .filter(\n",
    "        pl.col('State')=='United States',\n",
    "        pl.col('Year') == 2023,\n",
    "        pl.col('Age Group') != 'All Ages',\n",
    "        pl.col('Sex') == 'All Sexes'\n",
    "    )\n",
    "    .group_by('Age Group')\n",
    "    .agg(pl.col('COVID-19 Deaths').sum())\n",
    "    .sort(by='COVID-19 Deaths', descending=True)\n",
    ")\n",
    "\n",
    "fig = px.bar(\n",
    "    covid_deaths_by_age, \n",
    "    x='Age Group', \n",
    "    y='COVID-19 Deaths', \n",
    "    title='COVID Deaths 2023 by Age Group - As of 9/27/23'\n",
    ")\n",
    "\n",
    "fig.update_layout(xaxis_title=None)\n",
    "fig.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "alignmentgroup": "True",
         "hovertemplate": "State=%{x}<br>COVID-19 Deaths=%{y}<extra></extra>",
         "legendgroup": "",
         "marker": {
          "color": "#636efa",
          "pattern": {
           "shape": ""
          }
         },
         "name": "",
         "offsetgroup": "",
         "orientation": "v",
         "showlegend": false,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "California",
          "Florida",
          "Texas",
          "Pennsylvania",
          "New York"
         ],
         "xaxis": "x",
         "y": [
          4914,
          4296,
          3478,
          2454,
          2186
         ],
         "yaxis": "y"
        }
       ],
       "layout": {
        "barmode": "relative",
        "legend": {
         "tracegroupgap": 0
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "COVID Deaths 2023 by Top 5 States - As of 9/27/23"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ],
         "title": {}
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "COVID-19 Deaths"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "covid_deaths_by_top_5_states = (\n",
    "    df\n",
    "    .filter(\n",
    "        pl.col('State') != 'United States',\n",
    "        pl.col('Year') == 2023,\n",
    "        pl.col('Age Group') == 'All Ages',\n",
    "        pl.col('Sex') == 'All Sexes'\n",
    "    )\n",
    "    .group_by('State')\n",
    "    .agg(pl.col('COVID-19 Deaths').sum())\n",
    "    .sort(by='COVID-19 Deaths', descending=True)\n",
    "    .head()\n",
    ")\n",
    "\n",
    "fig = px.bar(\n",
    "    covid_deaths_by_top_5_states, \n",
    "    x='State', \n",
    "    y='COVID-19 Deaths', \n",
    "    title='COVID Deaths 2023 by Top 5 States - As of 9/27/23',\n",
    ")\n",
    "\n",
    "fig.update_layout(xaxis_title=None)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "alignmentgroup": "True",
         "hovertemplate": "Sex=%{x}<br>COVID-19 Deaths=%{y}<extra></extra>",
         "legendgroup": "",
         "marker": {
          "color": "#636efa",
          "pattern": {
           "shape": ""
          }
         },
         "name": "",
         "offsetgroup": "",
         "orientation": "v",
         "showlegend": false,
         "textangle": 0,
         "textfont": {
          "size": 12
         },
         "textposition": "inside",
         "texttemplate": "%{y:.2s}",
         "type": "bar",
         "width": 0.3,
         "x": [
          "Male",
          "Female"
         ],
         "xaxis": "x",
         "y": [
          26585,
          25099
         ],
         "yaxis": "y"
        }
       ],
       "layout": {
        "barmode": "relative",
        "legend": {
         "tracegroupgap": 0
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "COVID Deaths 2023 by Sex - As of 9/27/23"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Sex"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "COVID-19 Deaths"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "covid_deaths_by_sex = (\n",
    "    df\n",
    "    .filter(\n",
    "        pl.col('State') == 'United States',\n",
    "        pl.col('Year') == 2023,\n",
    "        pl.col('Age Group') == 'All Ages',\n",
    "        pl.col('Sex') != 'All Sexes'\n",
    "    )\n",
    "    .group_by('Sex')\n",
    "    .agg(pl.col('COVID-19 Deaths').sum())\n",
    "    .sort(by='COVID-19 Deaths', descending=True)\n",
    "    .head()\n",
    ")\n",
    "\n",
    "fig = px.bar(\n",
    "    covid_deaths_by_sex, \n",
    "    x='Sex', \n",
    "    y='COVID-19 Deaths', \n",
    "    title='COVID Deaths 2023 by Sex - As of 9/27/23',\n",
    "    text_auto='.2s'\n",
    ")\n",
    "\n",
    "fig.update_layout(xaxis_title=None)\n",
    "fig.update_traces(width = 0.3, textfont_size=12, textangle=0, textposition='inside')\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=East South Central<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Mississippi",
          "Kentucky",
          "Tennessee",
          "Alabama"
         ],
         "legendgroup": "East South Central",
         "marker": {
          "color": "#636efa",
          "size": [
           1459,
           2729,
           3690,
           1970
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "East South Central",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          434,
          1038,
          1389,
          640
         ],
         "xaxis": "x",
         "y": [
          0,
          11,
          11,
          0
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=Mountain<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Utah",
          "Idaho",
          "New Mexico",
          "Wyoming",
          "Nevada",
          "Colorado",
          "Montana",
          "Arizona"
         ],
         "legendgroup": "Mountain",
         "marker": {
          "color": "#EF553B",
          "size": [
           393,
           231,
           703,
           20,
           1302,
           1498,
           114,
           2606
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "Mountain",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          22,
          32,
          106,
          0,
          125,
          473,
          0,
          841
         ],
         "xaxis": "x",
         "y": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=New England<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Rhode Island",
          "Connecticut",
          "Massachusetts",
          "Maine",
          "Vermont",
          "New Hampshire"
         ],
         "legendgroup": "New England",
         "marker": {
          "color": "#00cc96",
          "size": [
           64,
           1469,
           2555,
           528,
           43,
           248
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "New England",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          33,
          389,
          1005,
          81,
          0,
          79
         ],
         "xaxis": "x",
         "y": [
          0,
          0,
          45,
          0,
          0,
          0
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=Pacific<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Alaska",
          "Hawaii",
          "California",
          "Oregon",
          "Washington"
         ],
         "legendgroup": "Pacific",
         "marker": {
          "color": "#ab63fa",
          "size": [
           0,
           415,
           14304,
           1240,
           2470
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "Pacific",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          0,
          20,
          4768,
          415,
          933
         ],
         "xaxis": "x",
         "y": [
          0,
          0,
          118,
          20,
          54
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=Mid-Atlantic<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Pennsylvania",
          "New York",
          "New Jersey"
         ],
         "legendgroup": "Mid-Atlantic",
         "marker": {
          "color": "#FFA15A",
          "size": [
           5011,
           5581,
           2705
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "Mid-Atlantic",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          2312,
          2026,
          1184
         ],
         "xaxis": "x",
         "y": [
          122,
          85,
          20
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=South Atlantic<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "West Virginia",
          "Delaware",
          "South Carolina",
          "Georgia",
          "Virginia",
          "Maryland",
          "Florida",
          "North Carolina"
         ],
         "legendgroup": "South Atlantic",
         "marker": {
          "color": "#19d3f3",
          "size": [
           1354,
           84,
           2106,
           3116,
           2428,
           2193,
           11032,
           4691
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "South Atlantic",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          299,
          66,
          882,
          1297,
          1111,
          795,
          4164,
          1837
         ],
         "xaxis": "x",
         "y": [
          0,
          0,
          0,
          0,
          0,
          0,
          116,
          22
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=West North Central<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Nebraska",
          "Iowa",
          "Minnesota",
          "North Dakota",
          "Kansas",
          "Missouri",
          "South Dakota"
         ],
         "legendgroup": "West North Central",
         "marker": {
          "color": "#FF6692",
          "size": [
           588,
           1004,
           1904,
           62,
           957,
           2599,
           123
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "West North Central",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          165,
          304,
          724,
          0,
          242,
          672,
          0
         ],
         "xaxis": "x",
         "y": [
          0,
          21,
          0,
          0,
          0,
          10,
          0
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=West South Central<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Arkansas",
          "Oklahoma",
          "Louisiana",
          "Texas"
         ],
         "legendgroup": "West South Central",
         "marker": {
          "color": "#B6E880",
          "size": [
           1497,
           2064,
           1141,
           9831
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "West South Central",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          249,
          646,
          413,
          3337
         ],
         "xaxis": "x",
         "y": [
          0,
          10,
          0,
          80
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=Others<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "New York City",
          "District of Columbia",
          "Puerto Rico"
         ],
         "legendgroup": "Others",
         "marker": {
          "color": "#FF97FF",
          "size": [
           2756,
           21,
           3257
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "Others",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          735,
          0,
          550
         ],
         "xaxis": "x",
         "y": [
          10,
          0,
          0
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "<b>%{hovertext}</b><br><br>Division=East North Central<br>COVID-19 Deaths=%{x}<br>Influenza Deaths=%{y}<br>Pneumonia Deaths=%{marker.size}<extra></extra>",
         "hovertext": [
          "Wisconsin",
          "Ohio",
          "Illinois",
          "Indiana",
          "Michigan"
         ],
         "legendgroup": "East North Central",
         "marker": {
          "color": "#FECB52",
          "size": [
           1730,
           4275,
           3978,
           2909,
           3806
          ],
          "sizemode": "area",
          "sizeref": 35.76,
          "symbol": "circle"
         },
         "mode": "markers",
         "name": "East North Central",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          591,
          1979,
          1316,
          820,
          1535
         ],
         "xaxis": "x",
         "y": [
          24,
          78,
          38,
          26,
          105
         ],
         "yaxis": "y"
        }
       ],
       "layout": {
        "legend": {
         "itemsizing": "constant",
         "title": {
          "text": "Division"
         },
         "tracegroupgap": 0
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "COVID-19, Influenza, and Pneumonia Deaths 2023 by US States and Divisions"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "COVID-19 Deaths"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Influenza Deaths"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from us_state_mappings import us_state_division_dict\n",
    "\n",
    "covid_deaths_vs_flu_deaths = (\n",
    "    df\n",
    "    .with_columns(\n",
    "        pl.col('State').replace_strict(us_state_division_dict, default='Others').alias('Division')\n",
    "    )\n",
    "    .filter(\n",
    "        pl.col('State') != 'United States',\n",
    "        pl.col('Age Group') != 'All Ages',\n",
    "        pl.col('Sex') != 'All Sexes',\n",
    "        pl.col('Year') == 2023\n",
    "    )\n",
    "    .group_by('State', 'Division')\n",
    "    .agg(\n",
    "        pl.col('COVID-19 Deaths').sum(),\n",
    "        pl.col('Influenza Deaths').sum(),\n",
    "        pl.col('Pneumonia Deaths').sum()\n",
    "    )\n",
    ")\n",
    "\n",
    "fig = px.scatter(\n",
    "    covid_deaths_vs_flu_deaths, \n",
    "    x='COVID-19 Deaths', \n",
    "    y='Influenza Deaths', \n",
    "    color='Division',\n",
    "    size='Pneumonia Deaths',\n",
    "    hover_name='State',\n",
    "    title='COVID-19, Influenza, and Pneumonia Deaths 2023 by US States and Divisions'\n",
    ")\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "hovertemplate": "Year=2022<br>Month=%{x}<br>COVID-19 Deaths=%{y}<extra></extra>",
         "legendgroup": "2022",
         "line": {
          "color": "#636efa",
          "dash": "solid",
          "shape": "spline"
         },
         "marker": {
          "symbol": "circle"
         },
         "mode": "lines",
         "name": "2022",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10,
          11,
          12
         ],
         "xaxis": "x",
         "y": [
          84011,
          50298,
          15627,
          6265,
          7633,
          9539,
          13393,
          14138,
          11125,
          9713,
          10036,
          14383
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "Year=2023<br>Month=%{x}<br>COVID-19 Deaths=%{y}<extra></extra>",
         "legendgroup": "2023",
         "line": {
          "color": "#EF553B",
          "dash": "solid",
          "shape": "spline"
         },
         "marker": {
          "symbol": "circle"
         },
         "mode": "lines",
         "name": "2023",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9
         ],
         "xaxis": "x",
         "y": [
          14877,
          8985,
          7593,
          5150,
          3518,
          2547,
          2295,
          3844,
          2875
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "Year=2020<br>Month=%{x}<br>COVID-19 Deaths=%{y}<extra></extra>",
         "legendgroup": "2020",
         "line": {
          "color": "#00cc96",
          "dash": "solid",
          "shape": "spline"
         },
         "marker": {
          "symbol": "circle"
         },
         "mode": "lines",
         "name": "2020",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10,
          11,
          12
         ],
         "xaxis": "x",
         "y": [
          6,
          25,
          7174,
          65550,
          38329,
          18026,
          31135,
          29911,
          19158,
          24930,
          53248,
          98174
         ],
         "yaxis": "y"
        },
        {
         "hovertemplate": "Year=2021<br>Month=%{x}<br>COVID-19 Deaths=%{y}<extra></extra>",
         "legendgroup": "2021",
         "line": {
          "color": "#ab63fa",
          "dash": "solid",
          "shape": "spline"
         },
         "marker": {
          "symbol": "circle"
         },
         "mode": "lines",
         "name": "2021",
         "orientation": "v",
         "showlegend": true,
         "type": "scatter",
         "x": [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10,
          11,
          12
         ],
         "xaxis": "x",
         "y": [
          105565,
          48570,
          23265,
          18805,
          14989,
          8024,
          11222,
          48821,
          63444,
          42606,
          32329,
          45623
         ],
         "yaxis": "y"
        }
       ],
       "layout": {
        "legend": {
         "title": {
          "text": "Year"
         },
         "tracegroupgap": 0,
         "traceorder": "reversed"
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "COVID-19 Deaths Monthly Trend - United States"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ],
         "dtick": 1,
         "title": {
          "text": "Month"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "COVID-19 Deaths"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "monthly_treand_by_year = (\n",
    "    df\n",
    "    .filter(\n",
    "        pl.col('State') == 'United States',\n",
    "        pl.col('Age Group') == 'All Ages',\n",
    "        pl.col('Sex') == 'All Sexes'\n",
    "    )\n",
    "    .group_by('Year', 'Month')\n",
    "    .agg(\n",
    "        pl.col('COVID-19 Deaths').sum(),\n",
    "    )\n",
    "    .sort(by='Month')\n",
    ")\n",
    "\n",
    "fig = px.line(\n",
    "    monthly_treand_by_year, \n",
    "    x='Month', \n",
    "    y='COVID-19 Deaths', \n",
    "    color='Year',\n",
    "    title='COVID-19 Deaths Monthly Trend - United States',\n",
    "    line_shape='spline'\n",
    ")\n",
    "\n",
    "fig.update_xaxes(dtick = 1)\n",
    "fig.update_layout(legend_traceorder='reversed')\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Detecting and handling outliers  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td></tr></thead><tbody><tr><td>5.1</td><td>3.5</td><td>1.4</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td></tr><tr><td>4.9</td><td>3.0</td><td>1.4</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td></tr><tr><td>4.7</td><td>3.2</td><td>1.3</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td></tr><tr><td>4.6</td><td>3.1</td><td>1.5</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td></tr><tr><td>5.0</td><td>3.6</td><td>1.4</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 6)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┬────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species ┆ species_id │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---     ┆ ---        │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str     ┆ i64        │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪═════════╪════════════╡\n",
       "│ 5.1          ┆ 3.5         ┆ 1.4          ┆ 0.2         ┆ setosa  ┆ 1          │\n",
       "│ 4.9          ┆ 3.0         ┆ 1.4          ┆ 0.2         ┆ setosa  ┆ 1          │\n",
       "│ 4.7          ┆ 3.2         ┆ 1.3          ┆ 0.2         ┆ setosa  ┆ 1          │\n",
       "│ 4.6          ┆ 3.1         ┆ 1.5          ┆ 0.2         ┆ setosa  ┆ 1          │\n",
       "│ 5.0          ┆ 3.6         ┆ 1.4          ┆ 0.2         ┆ setosa  ┆ 1          │\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴─────────┴────────────┘"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars as pl\n",
    "import plotly \n",
    "df = pl.from_pandas(plotly.data.iris())\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "alignmentgroup": "True",
         "hovertemplate": "sepal_width=%{y}<extra></extra>",
         "legendgroup": "",
         "marker": {
          "color": "#636efa"
         },
         "name": "",
         "notched": false,
         "offsetgroup": "",
         "orientation": "v",
         "showlegend": false,
         "type": "box",
         "x0": " ",
         "xaxis": "x",
         "y": [
          3.5,
          3,
          3.2,
          3.1,
          3.6,
          3.9,
          3.4,
          3.4,
          2.9,
          3.1,
          3.7,
          3.4,
          3,
          3,
          4,
          4.4,
          3.9,
          3.5,
          3.8,
          3.8,
          3.4,
          3.7,
          3.6,
          3.3,
          3.4,
          3,
          3.4,
          3.5,
          3.4,
          3.2,
          3.1,
          3.4,
          4.1,
          4.2,
          3.1,
          3.2,
          3.5,
          3.1,
          3,
          3.4,
          3.5,
          2.3,
          3.2,
          3.5,
          3.8,
          3,
          3.8,
          3.2,
          3.7,
          3.3,
          3.2,
          3.2,
          3.1,
          2.3,
          2.8,
          2.8,
          3.3,
          2.4,
          2.9,
          2.7,
          2,
          3,
          2.2,
          2.9,
          2.9,
          3.1,
          3,
          2.7,
          2.2,
          2.5,
          3.2,
          2.8,
          2.5,
          2.8,
          2.9,
          3,
          2.8,
          3,
          2.9,
          2.6,
          2.4,
          2.4,
          2.7,
          2.7,
          3,
          3.4,
          3.1,
          2.3,
          3,
          2.5,
          2.6,
          3,
          2.6,
          2.3,
          2.7,
          3,
          2.9,
          2.9,
          2.5,
          2.8,
          3.3,
          2.7,
          3,
          2.9,
          3,
          3,
          2.5,
          2.9,
          2.5,
          3.6,
          3.2,
          2.7,
          3,
          2.5,
          2.8,
          3.2,
          3,
          3.8,
          2.6,
          2.2,
          3.2,
          2.8,
          2.8,
          2.7,
          3.3,
          3.2,
          2.8,
          3,
          2.8,
          3,
          2.8,
          3.8,
          2.8,
          2.8,
          2.6,
          3,
          3.4,
          3.1,
          3,
          3.1,
          3.1,
          3.1,
          2.7,
          3.2,
          3.3,
          3,
          2.5,
          3,
          3.4,
          3
         ],
         "y0": " ",
         "yaxis": "y"
        }
       ],
       "layout": {
        "boxmode": "group",
        "legend": {
         "tracegroupgap": 0
        },
        "margin": {
         "t": 60
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "width": 500,
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ]
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "sepal_width"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import plotly.express as px\n",
    "\n",
    "fig = px.box(df, y='sepal_width', width=500)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (4, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td></tr></thead><tbody><tr><td>5.7</td><td>4.4</td><td>1.5</td><td>0.4</td><td>&quot;setosa&quot;</td><td>1</td></tr><tr><td>5.2</td><td>4.1</td><td>1.5</td><td>0.1</td><td>&quot;setosa&quot;</td><td>1</td></tr><tr><td>5.5</td><td>4.2</td><td>1.4</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td></tr><tr><td>5.0</td><td>2.0</td><td>3.5</td><td>1.0</td><td>&quot;versicolor&quot;</td><td>2</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (4, 6)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬────────────┬────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species    ┆ species_id │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---        ┆ ---        │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str        ┆ i64        │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪════════════╪════════════╡\n",
       "│ 5.7          ┆ 4.4         ┆ 1.5          ┆ 0.4         ┆ setosa     ┆ 1          │\n",
       "│ 5.2          ┆ 4.1         ┆ 1.5          ┆ 0.1         ┆ setosa     ┆ 1          │\n",
       "│ 5.5          ┆ 4.2         ┆ 1.4          ┆ 0.2         ┆ setosa     ┆ 1          │\n",
       "│ 5.0          ┆ 2.0         ┆ 3.5          ┆ 1.0         ┆ versicolor ┆ 2          │\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴────────────┴────────────┘"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q1 = pl.col('sepal_width').quantile(0.25)\n",
    "q3 = pl.col('sepal_width').quantile(0.75)\n",
    "iqr = q3 - q1\n",
    "threshold = 1.5\n",
    "lower_limit = q1 - iqr * threshold\n",
    "upper_limit = q3 + iqr * threshold\n",
    "\n",
    "df.filter(\n",
    "    (pl.col('sepal_width') < lower_limit) | (pl.col('sepal_width') > upper_limit)\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (0, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td></tr></thead><tbody></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (0, 6)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┬────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species ┆ species_id │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---     ┆ ---        │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str     ┆ i64        │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪═════════╪════════════╡\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴─────────┴────────────┘"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "is_outlier_iqr = (pl.col('sepal_width') < lower_limit) | (pl.col('sepal_width') > upper_limit)\n",
    "df_iqr_outlier_removed = (\n",
    "    df\n",
    "    .filter(is_outlier_iqr.not_())\n",
    ")\n",
    "df_iqr_outlier_removed.filter(is_outlier_iqr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (0, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td></tr></thead><tbody></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (0, 6)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┬────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species ┆ species_id │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---     ┆ ---        │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str     ┆ i64        │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪═════════╪════════════╡\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴─────────┴────────────┘"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_iqr_outlier_replaced = (\n",
    "    df\n",
    "    .with_columns(\n",
    "        pl.when(is_outlier_iqr)\n",
    "        .then(pl.col('sepal_width').median())\n",
    "        .otherwise(pl.col('sepal_width'))\n",
    "        .alias('sepal_width')\n",
    "    )\n",
    ")\n",
    "df_iqr_outlier_replaced.filter(is_outlier_iqr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th><th>sepal_width_zscore</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>f64</td></tr></thead><tbody><tr><td>5.1</td><td>3.5</td><td>1.4</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td><td>1.028611</td></tr><tr><td>4.9</td><td>3.0</td><td>1.4</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td><td>-0.12454</td></tr><tr><td>4.7</td><td>3.2</td><td>1.3</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td><td>0.33672</td></tr><tr><td>4.6</td><td>3.1</td><td>1.5</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td><td>0.10609</td></tr><tr><td>5.0</td><td>3.6</td><td>1.4</td><td>0.2</td><td>&quot;setosa&quot;</td><td>1</td><td>1.259242</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 7)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┬────────────┬─────────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species ┆ species_id ┆ sepal_width_zsc │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---     ┆ ---        ┆ ore             │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str     ┆ i64        ┆ ---             │\n",
       "│              ┆             ┆              ┆             ┆         ┆            ┆ f64             │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪═════════╪════════════╪═════════════════╡\n",
       "│ 5.1          ┆ 3.5         ┆ 1.4          ┆ 0.2         ┆ setosa  ┆ 1          ┆ 1.028611        │\n",
       "│ 4.9          ┆ 3.0         ┆ 1.4          ┆ 0.2         ┆ setosa  ┆ 1          ┆ -0.12454        │\n",
       "│ 4.7          ┆ 3.2         ┆ 1.3          ┆ 0.2         ┆ setosa  ┆ 1          ┆ 0.33672         │\n",
       "│ 4.6          ┆ 3.1         ┆ 1.5          ┆ 0.2         ┆ setosa  ┆ 1          ┆ 0.10609         │\n",
       "│ 5.0          ┆ 3.6         ┆ 1.4          ┆ 0.2         ┆ setosa  ┆ 1          ┆ 1.259242        │\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴─────────┴────────────┴─────────────────┘"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_zscore = (\n",
    "    df.with_columns(\n",
    "       sepal_width_zscore=(pl.col('sepal_width') - pl.col('sepal_width').mean()) / pl.col('sepal_width').std()\n",
    "    )\n",
    ")\n",
    "df_zscore.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "is_outlier_z_score = (pl.col('sepal_width_zscore') > 3) | (pl.col('sepal_width_zscore') < -3)\n",
    "df_zscore_outliers_removed = df_zscore.filter(is_outlier_z_score.not_())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (1, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th><th>sepal_width_zscore</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>f64</td></tr></thead><tbody><tr><td>5.7</td><td>4.4</td><td>1.5</td><td>0.4</td><td>&quot;setosa&quot;</td><td>1</td><td>3.104284</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (1, 7)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┬────────────┬─────────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species ┆ species_id ┆ sepal_width_zsc │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---     ┆ ---        ┆ ore             │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str     ┆ i64        ┆ ---             │\n",
       "│              ┆             ┆              ┆             ┆         ┆            ┆ f64             │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪═════════╪════════════╪═════════════════╡\n",
       "│ 5.7          ┆ 4.4         ┆ 1.5          ┆ 0.4         ┆ setosa  ┆ 1          ┆ 3.104284        │\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴─────────┴────────────┴─────────────────┘"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_zscore.filter(is_outlier_z_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (0, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th><th>sepal_width_zscore</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>f64</td></tr></thead><tbody></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (0, 7)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┬────────────┬─────────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species ┆ species_id ┆ sepal_width_zsc │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---     ┆ ---        ┆ ore             │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str     ┆ i64        ┆ ---             │\n",
       "│              ┆             ┆              ┆             ┆         ┆            ┆ f64             │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪═════════╪════════════╪═════════════════╡\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴─────────┴────────────┴─────────────────┘"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_zscore_outliers_removed.filter(is_outlier_z_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_zscore_outliers_replaced = (\n",
    "    df_zscore\n",
    "    .with_columns(\n",
    "        pl.when(is_outlier_z_score)\n",
    "        .then(pl.col('sepal_width').mean())\n",
    "        .otherwise(pl.col('sepal_width'))\n",
    "        .alias('sepal_width')\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (1, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>sepal_length</th><th>sepal_width</th><th>petal_length</th><th>petal_width</th><th>species</th><th>species_id</th><th>sepal_width_zscore</th></tr><tr><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>f64</td></tr></thead><tbody><tr><td>5.7</td><td>3.054</td><td>1.5</td><td>0.4</td><td>&quot;setosa&quot;</td><td>1</td><td>3.104284</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (1, 7)\n",
       "┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┬────────────┬─────────────────┐\n",
       "│ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width ┆ species ┆ species_id ┆ sepal_width_zsc │\n",
       "│ ---          ┆ ---         ┆ ---          ┆ ---         ┆ ---     ┆ ---        ┆ ore             │\n",
       "│ f64          ┆ f64         ┆ f64          ┆ f64         ┆ str     ┆ i64        ┆ ---             │\n",
       "│              ┆             ┆              ┆             ┆         ┆            ┆ f64             │\n",
       "╞══════════════╪═════════════╪══════════════╪═════════════╪═════════╪════════════╪═════════════════╡\n",
       "│ 5.7          ┆ 3.054       ┆ 1.5          ┆ 0.4         ┆ setosa  ┆ 1          ┆ 3.104284        │\n",
       "└──────────────┴─────────────┴──────────────┴─────────────┴─────────┴────────────┴─────────────────┘"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_zscore_outliers_replaced.filter(is_outlier_z_score)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
